﻿namespace Helper
{
    using System;
    using System.IO;

    public static class TraceLog
    {
        private static bool _newLine = true;
        public const string c_LogFile = "log.txt";
        public static bool WriteToFile = false;

        public static void Error(string format, params object[] args)
        {
            string message = GetMessage(format, args);
            DateTime time = DateTime.Now.ToLocalTime();
            ConsoleColor foregroundColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Red;
            if (!_newLine)
            {
                WriteToLogFile("\r\n");
                Console.WriteLine();
            }
            string str2 = string.Format("[{0}]: {1}", time, message);
            WriteToLogFile(str2 + "\r\n");
            Console.WriteLine(str2);
            Console.ForegroundColor = foregroundColor;
            _newLine = true;
        }

        private static string GetMessage(string format, object[] args)
        {
            if (args.Length > 0)
            {
                return string.Format(format, args);
            }
            return format;
        }

        public static void Print(string format, params object[] args)
        {
            string message = GetMessage(format, args);
            DateTime time = DateTime.Now.ToLocalTime();
            ConsoleColor foregroundColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Gray;
            if (_newLine)
            {
                string str2 = string.Format("[{0}]: ", time);
                Console.Write(str2);
                WriteToLogFile(str2);
                _newLine = false;
            }
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(message);
            WriteToLogFile(message);
            Console.ForegroundColor = foregroundColor;
        }

        public static void PrintLn(string format, params object[] args)
        {
            Print(format, args);
            WriteToLogFile("\r\n");
            Console.WriteLine();
            _newLine = true;
        }

        private static void WriteToLogFile(string str)
        {
            if (WriteToFile && !string.IsNullOrEmpty(str))
            {
                str = str.Replace("\b", "");
                StreamWriter writer = new StreamWriter("log.txt", true);
                writer.Write(str);
                writer.Flush();
                writer.Close();
            }
        }
    }
}

